package com.qf.question.service.impl;

import com.qf.question.dao.QuestionMapper;
import com.qf.question.entity.Question;
import com.qf.question.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 描述：
 *
 * @author ash-lei
 * @date 2021/10/14 11:10
 */
@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public Question findById(Integer id) {
        return questionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Question> findByType(Question question) {

        //List<Question> byType = questionMapper.findByType(type);
        Example example = createExample(question);
        //return byType;
        List<Question> questions = questionMapper.selectByExample(example);
        return questions;

    }

    public Example createExample(Question question){
        Example example=new Example(Question.class);
        Example.Criteria criteria = example.createCriteria();
        if(question!=null){
            // 用户id主键
            if(!StringUtils.isEmpty(question.getPkQid())){
                criteria.andEqualTo("pkQid",question.getPkQid());
            }
            // 手机号
            if(!StringUtils.isEmpty(question.getUkTitle())){
                criteria.andEqualTo("ukTitle",question.getUkTitle());
            }
            // 密码（md5+盐）
            if(!StringUtils.isEmpty(question.getReply())){
                criteria.andEqualTo("reply",question.getReply());
            }
            // 用户中文名
            if(!StringUtils.isEmpty(question.getKid())){
                criteria.andEqualTo("kid",question.getKid());
            }

        }
        return example;
    }
}
